package org.hisp.dhis.wsclient.j2me.javarosa;

import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Vector;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;

import org.hisp.dhis.wsclient.dao.PeriodDAO;
import org.hisp.dhis.wsclient.dao.mockup.MockupPeriodDAO;
import org.hisp.dhis.wsclient.model.Dataset;
import org.hisp.dhis.wsclient.model.OrganisationUnit;
import org.hisp.dhis.wsclient.model.Period;
import org.javarosa.core.Context;
import org.javarosa.core.api.Constants;
import org.javarosa.core.api.IActivity;
import org.javarosa.core.api.IShell;

import de.enough.polish.ui.Choice;
import de.enough.polish.ui.ChoiceItem;

public class SelectPeriodActivity
    implements IActivity, CommandListener
{
    public static final String CMD_CANCEL = "cancel";
    private IShell shell;
    private Keys keys;
    private Context context;

    private FilteredListContains list;

    private Command cmdCancel;
    private Command cmdSelect;

    private PeriodDAO periodDAO;

    private Vector periods;

    public SelectPeriodActivity( IShell shell, Keys keys )
    {
        setShell( shell );
        this.keys = keys;

        cmdSelect = new Command( "Select period", Command.OK, 0 );
        cmdCancel = new Command( "Cancel", Command.CANCEL, 0 );

        periodDAO = new MockupPeriodDAO();
    }

    public void contextChanged( Context globalContext )
    {
        // TODO Auto-generated method stub

    }

    public void destroy()
    {
        // TODO Auto-generated method stub

    }

    public Context getActivityContext()
    {
        // TODO Auto-generated method stub
        return null;
    }

    public void halt()
    {
        // TODO Auto-generated method stub

    }

    public void resume( Context globalContext )
    {
        shell.setDisplay( this, Util.createIView( list ) );
    }

    public void setShell( IShell shell )
    {
        this.shell = shell;
    }

    public void start( Context context )
    {
        this.context = context;

        OrganisationUnit orgunit = (OrganisationUnit) context.getElement( keys.ORGUNIT );
        Dataset dataset = (Dataset) context.getElement( keys.DATASET );

        periods = periodDAO.listPeriods( orgunit, dataset );
        list = createScreen( orgunit, dataset, periods );
        shell.setDisplay( this, Util.createIView( list ) );
    }

    private FilteredListContains createScreen( OrganisationUnit orgunit, Dataset dataset, Vector periods )
    {
        String title = "Select period for " + dataset.getName();

        //#style .filteredListScreen, .listScreen, .baseScreen
        FilteredListContains list = new FilteredListContains( title, FilteredListContains.IMPLICIT );

        list.addCommand( cmdSelect );
        list.addCommand( cmdCancel );

        Period period;
        for ( Enumeration e = periods.elements(); e.hasMoreElements(); )
        {
            period = (Period) e.nextElement();
            //#style .filteredListItem, .listItem, .item
            ChoiceItem choice = new ChoiceItem( period.getName(), null, Choice.IMPLICIT );
            choice.setDefaultCommand( cmdSelect );
            list.append( choice );
        }
        list.setCommandListener( this );

        return list;
    }

    public void commandAction( Command cmd, Displayable display )
    {
        if ( cmd == FilteredListContains.SELECT_COMMAND )
        {
            Period selectedPeriod = (Period) periods.elementAt( list.getSelectedIndex() );

            Hashtable returnVals = new Hashtable();
            returnVals.put( keys.PERIOD, selectedPeriod );
            shell.returnFromActivity( this, Constants.ACTIVITY_COMPLETE, returnVals );

        }
        else if ( cmd == cmdCancel )
        {
            shell.returnFromActivity( this, Constants.ACTIVITY_CANCEL, null );
        }

    }
}
